package com.example.cqcet_demo.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.cqcet_demo.R;
import com.example.cqcet_demo.bean.Page;
import com.example.cqcet_demo.uitls.ImageLoader;

import java.util.List;

import okhttp3.OkHttpClient;

/**
 * Created by Mr.z on 2017/6/20.
 */

public class MyXlistViewHomeAdapter extends BaseAdapter {

    private List<Page> items;
    private Context context;
    private OkHttpClient okHttpClient;


    public MyXlistViewHomeAdapter(Context context, List<Page> items, ListView listView){
        this.context = context;
        this.items = items;
        okHttpClient = new OkHttpClient();
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHoder viewHoder;
        if(convertView==null){
            convertView = View.inflate(context, R.layout.home_item,null);
            viewHoder = new ViewHoder();
            viewHoder.iv_home_img = (ImageView)convertView.findViewById(R.id.iv_home_img);
            viewHoder.tv_home_title = (TextView)convertView.findViewById(R.id.tv_home_title);
            viewHoder.tv_home_date = (TextView)convertView.findViewById(R.id.tv_home_date);
            convertView.setTag(viewHoder);
        }else{
            viewHoder = (ViewHoder) convertView.getTag();
        }
        Page p = items.get(position);
        String url = items.get(position).getImgUrl();
        ImageLoader imageLoader=new ImageLoader(context);
        imageLoader.DisplayImage(url, viewHoder.iv_home_img);

        viewHoder.tv_home_title.setText(p.getTitle());
        viewHoder.tv_home_date.setText(p.getDate());

        return convertView;
    }
    static class ViewHoder{
        ImageView iv_home_img;
        TextView tv_home_title;
        TextView tv_home_date;
    }


}
